/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */

package com.ykht.proj.coupon.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.security.util.SecurityUtils;
import com.ykht.proj.coupon.entity.FxWeightInfo;
import com.ykht.proj.coupon.service.FxWeightInfoService;
import com.ykht.proj.coupon.vo.FxWeightInfoEditVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 权重详情表
 *
 * @author proj code generator
 * @date 2021-04-25 18:47:54
 */
@RestController
@AllArgsConstructor
@RequestMapping("/fxweightinfo" )
@Api(value = "fxweightinfo", tags = "权重详情管理")
@Slf4j
public class FxWeightInfoController {

    private final  FxWeightInfoService fxWeightInfoService;


	@ApiOperation(value = "权重规则详情", notes = "规则列表")
	@GetMapping("/list")
	public R<List<FxWeightInfoEditVo>> list() {
		log.info("->权重规则详情");
		List<FxWeightInfoEditVo> list = new ArrayList<>();
		//根据类型进行分类
		Map<String, List<FxWeightInfo>> map = fxWeightInfoService.list().stream().collect(Collectors.groupingBy(FxWeightInfo::getWeightType));
		map.forEach((k,v)->{
			FxWeightInfoEditVo vo = new FxWeightInfoEditVo();
			vo.setWeightType(k);
			v = v.stream().sorted(Comparator.comparing(FxWeightInfo::getLevelLeft)).collect(Collectors.toList());
			vo.setInfo(v);
			list.add(vo);
		});
		return R.ok(list);
	}

    @ApiOperation(value = "通过id查询权重详情", notes = "通过id查询")
    @GetMapping("/{id}" )
//    @PreAuthorize("@pms.hasPermission('coupon_fxweightinfo_view')" )
    public R<FxWeightInfo> getById(@PathVariable("id" ) Long id) {
        return R.ok(fxWeightInfoService.getById(id));
    }

    @ApiOperation(value = "新增权重规则")
    @PostMapping
//    @PreAuthorize("@pms.hasPermission('coupon_fxweightinfo_add')" )
    public R save(@RequestBody FxWeightInfo fxWeightInfo) {
		fxWeightInfo.setCreateTime(LocalDateTime.now());
		fxWeightInfo.setCreateUser(SecurityUtils.getUserId());
//		return fxWeightInfoService.saveAndCheck(fxWeightInfo);
		return R.ok(fxWeightInfoService.save(fxWeightInfo));
    }

    @ApiOperation(value = "修改权重详情")
    @PutMapping
//    @PreAuthorize("@pms.hasPermission('coupon_fxweightinfo_edit')" )
    public R updateById(@RequestBody FxWeightInfo fxWeightInfo) {
		fxWeightInfo.setUpdateTime(LocalDateTime.now());
		fxWeightInfo.setUpdateUser(SecurityUtils.getUserId());
		return R.ok(fxWeightInfoService.update(fxWeightInfo, Wrappers.lambdaUpdate(FxWeightInfo.class).eq(FxWeightInfo::getId,fxWeightInfo.getId()).set(fxWeightInfo.getLevelRight()==null,FxWeightInfo::getLevelRight,null)));
    }

    @ApiOperation(value = "通过id删除权重详情")
    @DeleteMapping("/{id}" )
//    @PreAuthorize("@pms.hasPermission('coupon_fxweightinfo_del')" )
    public R removeById(@PathVariable Long id) {
        return R.ok(fxWeightInfoService.removeById(id));
    }

}
